﻿using System;
using System.Web.UI.WebControls;
using System.Data;

namespace PreContractWeb.Task
{
    public partial class QuerySearchPreContract : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BEGDC.PreContract.BLL.Authentication.PCIdentity user = (BEGDC.PreContract.BLL.Authentication.PCIdentity)Page.User.Identity;

                if (Page.User.IsInRole(BEGDC.PreContract.BLL.Role.EnumRole.Admin.ToString()))
                {
                    BaseFunction.BindProvinceDropDownList(dpProvinces, string.Empty, true);
                }
                else
                {
                    BaseFunction.BindProvinceDropDownList(dpProvinces, string.Empty, true, user.EmployeeId.ToString());
                }

                if (Page.User.IsInRole(BEGDC.PreContract.BLL.Role.EnumRole.Admin.ToString()))
                {
                    BaseFunction.BindCATDropDownList(dpCAT, string.Empty, true);
                }
                else
                {
                    BaseFunction.BindCATDropDownList(dpCAT, string.Empty, true, user.EmployeeId.ToString());
                }

                this.pgPreContractInfo.Visible = false;
            }
        }

        private void BindDataGrid()
        {
            BEGDC.PreContract.BLL.IPreContractInfo iPreContractInfo = (BEGDC.PreContract.BLL.IPreContractInfo)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_PreContractInfo_Service];
            BEGDC.PreContract.BLL.Authentication.PCIdentity user = (BEGDC.PreContract.BLL.Authentication.PCIdentity)Page.User.Identity;

            string dpProvinces = this.dpProvinces.SelectedValue;
            string dpCAT = this.dpCAT.SelectedValue;
            string dpFeedbackStatus = this.dpFeedbackStatus.SelectedValue;
            string dpExistMonth = this.dpExistMonth.SelectedValue;

            bool isAdmin = Page.User.IsInRole(BEGDC.PreContract.BLL.Role.EnumRole.Admin.ToString());

            DataTable dt = iPreContractInfo.QuickSearchPreContract(dpProvinces, dpCAT, dpFeedbackStatus, dpExistMonth, user.EmployeeId, isAdmin);

            this.gvPreContractInfo.DataSource = dt.DefaultView;
            this.gvPreContractInfo.DataBind();

            this.pgPreContractInfo.Visible = true;
            this.pgPreContractInfo.BindtoGridView(this.gvPreContractInfo, dt.DefaultView);
            if (dt.Rows.Count == 0)
            {
                this.pgPreContractInfo.Visible = false;
            }
        }

        protected void btnQuickSearch_Click(object sender, EventArgs e)
        {
            this.pgPreContractInfo.GVCurrentPageIndex = 1;
            this.BindDataGrid();
        }

        protected void gvPreContractInfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex >= 0)
            {
                e.Row.Cells[4].Text = "<a onclick=\"javascript:ShowPage('" + e.Row.Cells[0].Text.Trim() + "');\">" + e.Row.Cells[4].Text.Trim() + "</a>";
            }
        }
    }
}